#include "Data.h"

Data::Data(string path)
{
	
			//BufferedReader f = new BufferedReader(new FileReader(path));
	  //      // input file name goes above
			//		
			//String line = f.readLine();
			//StringTokenizer st;
			//while (line != null)
			//{
			//	st = new StringTokenizer(line);	
			//	int n = st.countTokens();
			//	attributeNum = n;
			//	double[] tuple = new double[n];
			//	for (int i = 0; i < n; i++){
			//		tuple[i] = Double.parseDouble(st.nextToken());
			//	}
			//	dataset.add(tuple);
			//	line = f.readLine();
			//}
		
		char line[LINE_LENGTH];  
		char *st;
		vector<double> tuple;
		ifstream fin(path.c_str());
		attributeNum = 0;
		while(fin.getline(line,LINE_LENGTH))
		{
			attributeNum = 0;
			st = strtok(line," ");
			tuple.clear();
			while( st != NULL )
			{
				//attributeNum++;
				tuple.push_back(atof(st));
				attributeNum = tuple.size();
				st = strtok(NULL," ");
			}
			dataset.push_back(*(new vector<double> (tuple)));
			
		}

		
}

void Data::normalize()
{
	int n = dataset.at(0).size();
	max = new double[n-2];
	min = new double[n-2];
		for (int i = 2; i < n; i++){
			max[i-2] = dataset.at(0).at(i);
			min[i-2] = dataset.at(0).at(i);
		}
		
		for (int i = 1; i < dataset.size(); i++){
			for (int j = 2; j < n; j++){
				if (dataset.at(i).at(j) > max[j-2])
					max[j-2] = dataset.at(i).at(j);
				if (dataset.at(i).at(j) < min[j-2])
					min[j-2] = dataset.at(i).at(j);							
			}
		}
		
		for (int i = 0; i < dataset.size(); i++){
			//vector<double> tuple = dataset.at(i);
			for (int j = 2; j < n; j++){
				dataset[i][j] = (dataset[i][j] - min[j-2])/(max[j-2] - min[j-2]);
			}
			//dataset[i]= tuple;
		}
}

void Data::normalize(double* min, double* max)
{
	int n = dataset.at(0).size();
		
	for (int i = 0; i < dataset.size(); i++){
		for (int j = 2; j < n; j++){
			dataset[i][j]= (dataset[i][j] - min[j-2])/(max[j-2] - min[j-2]);
		}
	}
}
double Data::calMisRecord(Network & network)
{
	int yes = 0;
	int no = 0;
	for (int i = 0; i < dataset.size(); i++){
		if (network.testTuple(dataset.at(i))){
			yes++;
		} else {
			no++;
		}
	}
	cout << yes << " : " << no << endl;
	return (double)yes / (yes+no);
}

Data::Data(vector<vector<double>> datasetNew)
{
	this->dataset = datasetNew;
	this->attributeNum = datasetNew.at(0).size();
}

Data::~Data(void)
{
}
